﻿namespace System.Security.Cryptography
{
    /// <summary>
    /// 构造 SymmetricCryptoService 需要的可选参数。
    /// </summary>
    public class SymmetricCryptoServiceOption
    {
        /// <summary>
        /// 指定需要创建的算法名称。默认是 Des
        /// </summary>
        public SymmetricAlgorithmName SymmetricAlgorithmName { get; set; } = SymmetricAlgorithmName.Des;

        /// <summary>
        /// 算法密钥。
        /// </summary>
        public string Password { get; set; } = "这是密钥。它可以是任意字符串。";

        /// <summary>
        /// 使用指定的 SymmetricAlgorithmName 和 Password 创建一个对称算法的实例。
        /// </summary>
        /// <returns></returns>
        internal SymmetricAlgorithm CreateAlgorithm()
        {
            SymmetricAlgorithm _algorithm = SymmetricAlgorithm.Create(SymmetricAlgorithmName.ToString())!;
            byte[] keyBytes = HashAlgorithmName.SHA512.ComputeHash(Password);
            _algorithm.Key = keyBytes.Take(_algorithm.Key.Length).ToArray();
            _algorithm.IV = keyBytes.TakeLast(_algorithm.IV.Length).Reverse().ToArray();
            return _algorithm;
        }
    }
}